package ian.android.lovewords.settings;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

public class MyNotify {

	public static int num = 0;

	private static AlarmManager getAlarmManager(Context ctx) {
		return (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
	}

	public static void setReminder(Context ctx, Boolean b, int time) {
		AlarmManager am = getAlarmManager(ctx);
		Intent receiver = new Intent(ctx, MyReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, receiver, 0);
		Log.i("Suck", "" + time);
		if (b) {
			Calendar c = Calendar.getInstance();
			// schedule an alarm
			Log.i("FUCK", Long.toString(c.getTimeInMillis()));

			// code to be execute in RealCase
			   
			  long nowTime =c.getTimeInMillis();
			  Log.i("FUCK",""+ nowTime);
			  c.set(Calendar.HOUR_OF_DAY, time);
			  c.set(Calendar.MINUTE, 0);
			  c.set(Calendar.SECOND, 0);
			  c.set(Calendar.MILLISECOND, 0); 
			  long clockTime = c.getTimeInMillis();
			  Log.i("FUCK",""+ clockTime);
			  if(clockTime<nowTime) 
			  		clockTime = clockTime + 86400000;
			  Log.i("FUCK",""+ clockTime);
			  am.setRepeating(AlarmManager.RTC_WAKEUP,
			  		clockTime, 86400000, pi);
			 

			// am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
			/*Log.i("suck", "" + c.getTimeInMillis());
			am.setRepeating(AlarmManager.RTC_WAKEUP,
					c.getTimeInMillis() + 1000, 10 * 1000, pi);*/
		} else {
			// cancel the current alarm
			am.cancel(pi);
		}
	}

	public static void setReminder(Context ctx, Boolean b, int time, int value) {
		AlarmManager am = getAlarmManager(ctx);
		Intent receiver = new Intent(ctx, MyReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, receiver, 0);
		num = value;
		if (b) {
			Calendar c = Calendar.getInstance();
			// schedule an alarm
			Log.i("FUCK", Long.toString(c.getTimeInMillis()));

			// code to be execute in RealCase
			   
			  long nowTime =c.getTimeInMillis();
			  c.set(Calendar.HOUR_OF_DAY, time);
			  c.set(Calendar.SECOND, 0);
			  c.set(Calendar.MILLISECOND, 0); 
			  long clockTime = c.getTimeInMillis();
			  if(clockTime<nowTime) 
			  		clockTime = clockTime + 86400000;
			  am.setRepeating(AlarmManager.RTC_WAKEUP,
			  		clockTime, 86400000, pi);
			

			// am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
			//Log.i("suck", "" + SystemClock.currentThreadTimeMillis());
			/*am.setRepeating(AlarmManager.RTC_WAKEUP,
					c.getTimeInMillis() + 1000, 10 * 1000, pi);*/
		} else {
			// cancel the current alarm
			am.cancel(pi);
		}
	}

	public static int TimeToInt(String time) {
		if (time == "1:00") {
			return 1;
		} else if (time == "2:00") {
			return 2;
		} else if (time == "3:00") {
			return 3;
		} else if (time == "4:00") {
			return 4;
		} else if (time == "5:00") {
			return 5;
		} else if (time == "6:00") {
			return 6;
		} else if (time == "7:00") {
			return 7;
		} else if (time == "8:00") {
			return 8;
		} else if (time == "9:00") {
			return 9;
		} else if (time == "10:00") {
			return 10;
		} else if (time == "11:00") {
			return 11;
		} else if (time == "12:00") {
			return 12;
		} else if (time == "13:00") {
			return 13;
		} else if (time == "14:00") {
			return 14;
		} else if (time == "15:00") {
			return 15;
		} else if (time == "16:00") {
			return 16;
		} else if (time == "17:00") {
			return 17;
		} else if (time == "18:00") {
			return 18;
		} else if (time == "19:00") {
			return 19;
		} else if (time == "20:00") {
			return 20;
		} else if (time == "21:00") {
			return 21;
		} else if (time == "22:00") {
			return 22;
		} else if (time == "23:00") {
			return 23;
		} else {
			return 0;
		}
	}

}
